//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';

import '../view_model/choose_service_group_view_model.dart';

class ChooseServiceGroupPage extends BaseStatefulMvvmWidget{
  List<dynamic> chooseList;

  ChooseServiceGroupPage({this.chooseList = const []});

  @override
  State<StatefulWidget> createState() => _ChooseServiceGroupState();

}

class _ChooseServiceGroupState extends BaseMvvmState<ChooseServiceGroupViewModel,ChooseServiceGroupPage>{
  @override
  Widget buildLoadingContentView() {
    return Stack(
      children: [
        Row(
          children: [
            ListView(
              children: List.generate(viewModel.groupList.length, (index) {
                var item = viewModel.groupList[index];
                return CommonText(item['name']).addToContainer(
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: viewModel.currentIndex==index?Colors.white:Colors.transparent
                  ),
                  padding: EdgeInsets.symmetric(vertical: 12)
                ).onTap(() {
                  setState(() {
                    viewModel.currentIndex = index;
                  });
                });
              }),
            ).addToContainer(color: Colours.bg_color).setWeight(1),


            (viewModel.groupList.isNotEmpty?ListView(
              children: [
                ... (( viewModel.groupList[viewModel.currentIndex]['user'] as List?)??[]).map((e) {
                  return Row(
                    children: [
                      CommonText(e['userName']).setWeight(1),
                      Icon(viewModel.isContain(e)?Icons.check_box:Icons.check_box_outline_blank,color: viewModel.isContain(e)?Colors.blue:Colors.grey,)
                    ],
                  ).addToContainer(
                      padding: EdgeInsets.symmetric(horizontal: 16,vertical: 14)
                  ).onTap(() {
                    viewModel.clickEmployee(e);
                  });
                }).toList().insertWidget(Gaps.dividerLine)
              ],
            ):Container()).setWeight(3)
          ],
        ).setLocation(left: 0,right: 0,top: 0,bottom: 0),

        if(viewModel.selectedList.isNotEmpty)
          TextButton(onPressed: (){
            Go().pop(result: viewModel.selectedList);
          }, child: CommonText('确定',textColor: Colors.white),style: TextButton.styleFrom(
            backgroundColor: Colors.blue,shape: StadiumBorder()
          ),).setLocation(left: 16,right: 16,bottom: 20)
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择通知人"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  ChooseServiceGroupViewModel createViewModel() {
    return ChooseServiceGroupViewModel(context,chooseList: widget.chooseList);
  }

  @override
  void onRetryClick() {
    viewModel.getListInfo();
  }

}